//
//  TL_FeedbackContactTableViewCell.m
//  WXReader
//
//  Created by Andrew on 2019/12/27.
//  Copyright © 2019 Andrew. All rights reserved.
//

#import "TL_FeedbackContactTableViewCell.h"

@interface TL_FeedbackContactTableViewCell () <UITextFieldDelegate> {
    UITextField *textField;
}

@end

@implementation TL_FeedbackContactTableViewCell

- (void)createSubviews {
    
    [self.contentView setBackgroundColor:[UIColor hex_F8F8F8]];
    
    UIView *backView = [TL_UIFactory view];
    backView.backgroundColor = [UIColor whiteColor];
    backView.layer.cornerRadius = 6;
    backView.layer.masksToBounds = YES;
    [self.contentView addSubview:backView];

    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_top).offset(16);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(52);
    }];
    
    textField = [TL_UIFactory textFieldWithBackgroundColor:nil placeholder:WXLocalizedString(@"在这里写下您的电子邮件地址") textColor:kBlackColor font:kMainFont delegate:self isNumber:NO];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.keyboardType = UIKeyboardTypeEmailAddress;
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:WXLocalizedString(@"在这里写下您的电子邮件地址")];
    [attributedString setAttributes:@{NSFontAttributeName:kMainFont, NSForegroundColorAttributeName:kGrayTextColor} range:NSMakeRange(0, attributedString.string.length)];
    textField.attributedPlaceholder = attributedString;
    [backView addSubview:textField];
    
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(backView).offset(kHalfMargin);
        make.right.mas_equalTo(backView).offset(-kHalfMargin);
    }];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([string isEqualToString:@""]) {
        return YES;
    }
    
    if (textField.text.length >= 30) {
        textField.text = [textField.text substringToIndex:30];
        return NO;
    }
    
    if (self.contactDidChange) {
        self.contactDidChange(textField.text);
    }
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    if (self.contactDidChange) {
        self.contactDidChange(@"");
    }
    return YES;
}

@end
